$(function(){
	//$("input").attr("maxlength","80");
	$("textarea").attr("maxlength","1000");
	//全选或全不选
	 $("span[name=checkAll]").live("click",function(){
	        var booleanParentSelect=$("span[name=checkAll]").children().hasClass("active");
	        if(!booleanParentSelect){
	        	$("span[name=checkSingle]").children().removeClass("active");
	        }else{
	        	$("span[name=checkSingle]").children().addClass("active");
	        }
	    });
	 //点击子项的选择
	 $("span[name=checkSingle]").live("click",function(){
		 if($("span[name=checkSingle]").children().length > 0){
				var count = 0;
				for (var int = 0; int < $("span[name=checkSingle]").children().length; int++) {
					if($("span[name=checkSingle]").children().eq(int).hasClass("active")){
						count ++;
					}
				}
				if($("span[name=checkSingle]").children().length == count){
					$("span[name=checkAll]").children().addClass("active");
				}else{
					$("span[name=checkAll]").children().removeClass("active");
				}
			}
	 });
	
	//弹窗全选或全不选
	 $("span[name=checkAll1]").live("click",function(){
	        var booleanParentSelect=$("span[name=checkAll1]").children().hasClass("active");
	        if(!booleanParentSelect){
	        	$("span[name=checkSingle1]").children().removeClass("active");
	        }else{
	        	$("span[name=checkSingle1]").children().addClass("active");
	        }
	    });
	 //弹窗点击子项的选择
	 $("span[name=checkSingle1]").live("click",function(){
		 if($("span[name=checkSingle1]").children().length > 0){
				var count = 0;
				for (var int = 0; int < $("span[name=checkSingle1]").children().length; int++) {
					if($("span[name=checkSingle1]").children().eq(int).hasClass("active")){
						count ++;
					}
				}
				if($("span[name=checkSingle1]").children().length == count){
					$("span[name=checkAll1]").children().addClass("active");
				}else{
					$("span[name=checkAll1]").children().removeClass("active");
				}
			}
	 });
});

var pagePath = $("#pagePath").val();
var resourcePath = $("#resourcePath").val();

function searchUserGroup(obj){
	$("#searchBody").html("");
	var html="";
	var name = $.trim($(obj).parent().find("input").eq(0).val());
	var code = $.trim($(obj).parent().find("input").eq(1).val());
	var data = {"name":name,"code":code};
	$().invoke(pagePath + "/settings/userGroup/searchUserGroup.json",data,[function(m,r){
		if(r.length < 1){
			html += "<div class='no-data '><img src='"+resourcePath+"/images/pig/no-data.png' alt=''/></div>";
		}else{
			$.each(r,function(i,evalue){
				var hangClass = 'ul-header bg-white';
			    if(i % 2 != 0){
			        hangClass = 'ul-header bg-second';
			    }
			    var stateClass = '';
			    if('LOCKED' == evalue.STATE){
			    	stateClass = 'on-off';
			    }else if('ACTIVATE' == evalue.STATE){
			    	stateClass = 'on-off active';
			    }
			    
			    html += "<div class='info-tr'>" +
							"<ul class= '"+hangClass+"'>" +
								"<li class='sw5'  style='width:5%'><span class='table-ck'><span name='checkSingle' class='check-box selected' id='"+evalue.ID+"'><i></i></span></span></li>" +
								"<li class='sw15' style='width:25%'>"+convertObjectToString(evalue.NAME)+"</li>" +
								"<li class='sw15' style='width:20%'>"+convertObjectToString(evalue.CODE)+"</li>" +
								"<li class='sw15' style='width:10%'>"+convertObjectToString(evalue.CREATEUSER)+"</li>" +
								"<li class='sw15' style='width:20%'>"+convertObjectToString(evalue.REMARK)+"</li>" +
								"<li class='sw15-2-0' style='width: 5%'><em  style='margin-left:-4px';onclick='changeState(this);'  class='"+stateClass+"'></em></li>" +
								"<li class='sw15-4-01 no-right' style='width:15%;position:relative'>" +
									"<a href='javascript:void(0);' class='edit0' onclick='showDialog(this);' style='position:absolute;margin-left:-66px;top:50%;margin-top:-12px'>" +
									"</a>" +
									"<a href='javascript:void(0);'  class='delet1' onclick='deleteData(this);' style='position:absolute;margin-left:-14px;top:50%;margin-top:-14px'></a>" +
									"<a href='javascript:void(0);'  class='shouquan' onclick='groupAuthorize(this);' style='position:absolute;margin-left:38px;top:50%;margin-top:-14px' ></a>" +
							"</ul>"+
						"</div>";
			    
			});
		}
		$("#searchBody").html(html);
	}]);
}


/**
 * 
 * @Title: showDialog   
 * @Description: 新增或修改弹窗数据加载
 * @param: @param obj      
 * @return: void      
 * @throws
 */
function showDialog(obj){
	var html="";
	$("#result").html("");
	if("add" == obj){
		$(".PopupTitle").html("新建用户组信息");
		$("#groupName").val("");
		$("#groupCode").val("");
		$("#groupRemark").val("");
		$("#groupId").val("");
		$("#add").modal("show");
	}else{
		$(".PopupTitle").html("修改用户组信息");
		var userGroupId = $(obj).parent().parent().find("span[name='checkSingle']").attr("id");
		var name = $(obj).parent().parent().children("li").eq(1).text();
		var code = $(obj).parent().parent().children("li").eq(2).text();
		var remark = $(obj).parent().parent().children("li").eq(4).text();
		$("#groupName").val(name);
		$("#groupCode").val(code);
		$("#groupRemark").val(remark);
		$("#groupId").val(userGroupId);
		$().invoke(pagePath + "/settings/userGroup/addOrModify.json",{"id":userGroupId},[function(m,r){
			var userGroup = r.userGroup;
			if("LOCKED" != userGroup.state){
				alert("该数据不允许修改");
			}else{
				$("#add").modal("show");
			}
		}]);
	}
}



/**
 * 
 * @Title: save   
 * @Description: 保存用户组以及用户所属用户组信息
 * @param:       
 * @return: void      
 * @throws
 */
function save(){
	var groupId = $("#groupId").val();
	//组名
	var name = $.trim($("#groupName").val());
	//编码
	var code = $.trim($("#groupCode").val());
	
	var remark = $.trim($("#groupRemark").val());
	//状态
	var state = "LOCKED";
	if(""== name){
		alert("请输入用户组名称");
		return;
	}else{
		var data = {"name":name,"code":code,"id":groupId,"state":state,"remark":remark};
		$().invoke(pagePath + "/settings/userGroup/saveGroup.json",data,[function(m,r){
			if(1==r){
				alert("操作成功");
				window.location.href = pagePath + "/settings/userGroup/getUserGroupManagerIndex.html";
			}
		}]);
	}
}

/**
 * 
 * @Title: groupAuthorize   
 * @Description: 点击授权加载信息   
 * @param: @param obj      
 * @return: void      
 * @throws
 */
function groupAuthorize(obj){
	var userGroupId = $(obj).parent().parent().find("span[name='checkSingle']").attr("id");
	if("" == userGroupId || undefined == userGroupId){
		alert("数据异常，不允许的操作");
		return;
	}
	$().invoke(pagePath + "/settings/userGroup/groupAuthorize.json",{"id":userGroupId},[function(m,r){
		$("#authorizeGroupUser").val(userGroupId);
		if(null != r.userGroup){
			var userGroup = r.userGroup;
			$("#sh").find(".s1").html(userGroup.name);
		}
//		if(r.allRole.length > 0){
			var left = r.allRole;
			var leftObj = $(".left-ch").children("ul");
			var rightObj = $(".right-ch").children("ul");
			var leftHtml ="";
			var rightHtml = "";
			var role = r.role;
			for (var i = 0; i < left.length; i++) {
				if(role.indexOf(left[i].ID) != -1){
					rightHtml += "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
				}else{
					leftHtml+= "<li><input type='hidden' value='"+left[i].ID+"'/><span>"+left[i].NAME+"</span></li>";
				}
			}
			
//		}
		leftObj.html(leftHtml);
		rightObj.html(rightHtml);
		authorize();
		$("#sh").modal("show");
	}]);
}

/**
 * 
 * @Title: saveAuthorize   
 * @Description: 保存授权信息
 * @param:       
 * @return: void      
 * @throws
 */
function saveAuthorize(){
	var userGroupId = $("#authorizeGroupUser").val();
	var dataList = $(".right-ch").children("ul").children();
	var str = "";
	var dataArr = new Array();
	for (var i = 0; i < dataList.length; i++) {
		var data = $(".right-ch").children("ul").children("li").children("input").eq(i).val();
		dataArr.push(data);
	}
	str = dataArr.join(",");
	$().invoke(pagePath + "/settings/userGroup/saveAuthorize.json",{"id":userGroupId,"str":str},[function(m,r){
		if(r > 0){
			alert("操作成功");
		}
	}]);
}


/**
 * @Title: authorize   
 * @Description: 授权js操作
 * @param:       
 * @return: void      
 * @throws
 */
function authorize(){
	 $('.right-ch li').on('click',function(){
         $(this).addClass('active');
     })
     $('.left-ch li').on('click',function(){
         $(this).addClass('active');
     })
     $('.left-icon').on('click',function(){

         $('.right-ch ul ').append($('.left-ch ul li.active'));
         $('.right-ch ul li.active').removeClass("active");
     })
     $('.right-icon').on('click',function(){
         $('.left-ch ul ').append($('.right-ch ul li.active'));
         $('.left-ch ul li.active').removeClass("active");
     })
}



function convertObjectToString(obj){
    return null==obj || "" == $.trim(obj) ? "-" : obj;
}
function convertObjectToDate(obj){
    return null==obj || "" == $.trim(obj) ? "-" : new Date(obj).Format("yyyy-MM-dd hh:mm:ss");
}


/**
*
* @Title: deleteData   
* @Description: 删除用户组
* @param: @param obj      
* @return: void      
* @throws
*/
function deleteData(obj){
	//获取所有name = childSelect的input
	var childBox = $("span[name=checkSingle]");
	if('multiple' == obj){
		var idsArr = new Array();
		var count = 0;
		for(var i= 0 ; i < childBox.length;i ++){
			if(childBox[i].getAttribute("class").indexOf("selected") < 0){
				idsArr.push(childBox[i].id);
				count++;
			}
		}
		if(idsArr.length < 1){
			alert("请先勾选需要删除的数据");
			return ;
		}
		var idStr = idsArr.join();
		var delFlag = window.confirm("确定要删除吗");
		if(delFlag == true){
			$().invoke(pagePath + "/settings/userGroup/deleteData.json",{"idStr":idStr},[function(m,r){
				alert("已选择【"+count+"】条，成功删除【 "+r +" 】条");
				window.location.href = pagePath + "/settings/userGroup/getUserGroupManagerIndex.html";
			}]);
		}
	}else{
		var idStr= $(obj).parent().parent().find("span[name='checkSingle']").attr("id");
		var delFlag = window.confirm("确定要删除吗");
		if(delFlag == true){
			$().invoke(pagePath + "/settings/userGroup/deleteData.json",{"idStr":idStr},[function(m,r){
				if(r == 0){
					alert("该记录已经不允许删除");
				}else{
					alert("删除成功");
					window.location.href = pagePath + "/settings/userGroup/getUserGroupManagerIndex.html";
				}
			}]);
		}
	}
}

/**
 * 
 * @Title: changeState   
 * @Description: 改变用户组状态
 * @param: @param obj      
 * @return: void      
 * @throws
 */
function changeState(obj){
	var userGroupId = $(obj).parent().parent().find("span[name='checkSingle']").attr("id");
	var boolean = $(obj).hasClass("active");
	var state = "";
	if("" == userGroupId || undefined == userGroupId){
		alert("数据异常，不允许的操作");
		return;
	}
	if(!boolean){
		state = "ACTIVATE";
	}else{
		state = "LOCKED";
	}
	$().invoke(pagePath + "/settings/userGroup/changeState.json",{"id":userGroupId,"state":state},[function(m,r){
		if(1== r){
			$(obj).parent().next().find("input").eq(3).val(state);
			var bo = $(obj).parent().children().attr("class") == "on-off";
			if(bo){
				$(obj).parent().children().removeClass();
				$(obj).parent().children().addClass("on-off active");
			}else{
				$(obj).parent().children().removeClass();
				$(obj).parent().children().addClass("on-off");
			}
//			alert("操作成功");
		}
	}]);
}


/**
 * 
 * @Title: dataOnOrOff   
 * @Description: 启用或禁用用户组
 * @param: @param opt      
 * @return: void      
 * @throws
 */
function dataOnOrOff(opt){
	var childBox = $("span[name=checkSingle]");
	var idsArr = new Array();
	var count = 0;
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox[i].getAttribute("class").indexOf("selected") < 0){
			idsArr.push(childBox[i].id);
			count++;
		}
	}
	if(idsArr.length < 1){
		alert("请先勾选需要操作的数据");
		return ;
	}
	var idStr = idsArr.join();
	var flag = window.confirm("确定要执行该操作吗");
	if(flag == true){
		$().invoke(pagePath + "/settings/userGroup/dataOnOrOff.json",{"idStr":idStr,"opt":opt},[function(m,r){
			if("on" == opt){
				alert("已选择【"+count+"】条，成功启用【 "+r +" 】条");
			}else{
				alert("已选择【"+count+"】条，成功停用【 "+r +" 】条");
			}
			window.location.href = pagePath + "/settings/userGroup/getUserGroupManagerIndex.html";
		}]);
	}
}


function getUserTitle(){
	//新建用户组弹窗，用户列表各个值显示title
	var li = $("#result").find("li");
	for (var i = 0; i < li.length; i++) {
		if(li.eq(i).hasClass("table-w170")){
			var value = li.eq(i).html();
			li.eq(i).attr('title',value);
		}
	}
}